import {AdminLogin} from "@/api/Login"
import {setTokenTime} from '@/utils/auth'
import router from "@/router"
import {ElMessage} from "element-plus";

export default {
    namespaced:true,
    state:()=>({
        token:localStorage.getItem('token') || '',
        Type:localStorage.getItem('type') || '',
        Id:localStorage.getItem('id') || '',
        OnePost:localStorage.getItem('onepost') || '',
        Loginer:localStorage.getItem('loginer') || '',
    }),
    mutations:{
        setToken(state,token){
            state.token=token
            localStorage.setItem('token',token)
        },
        setLoginType(state,type){
            state.Type=type
            localStorage.setItem('type',type)
        },
        setLoginId(state,id){
            state.Id=id
            localStorage.setItem('id',id)
        },
        setOnePost(state,data){
            state.OnePost=data
            localStorage.setItem('onepost',JSON.stringify(data))
        },
        setLoginer(state,data){
            state.OnePost=data
            localStorage.setItem('loginer',JSON.stringify(data))
        }
    },
    actions:{
        ALogin({commit},userinfo){
            return new Promise((resolve,reject)=>{
                AdminLogin(userinfo).then(res=>{
                    if (res.code===20041){
                        commit('setToken',res.data.token)
                        commit('setLoginType','管理员')
                        commit('setLoginer',res.data)
                        setTokenTime()
                        ElMessage({
                            message: res.msg,
                            type: 'success',
                        })
                        router.replace('/')
                    }else {
                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }
                    resolve()
                }).catch(err=>{
                    reject(err)
                })
            })
        },
        SetToken({commit},token){
            commit('setToken',token)
        },
        SetLoginer({commit},data){
            commit('setLoginer',data)
        },
        AdminLoginOut({commit}){
            commit('setToken','')
            commit('setLoginType','')
            commit('setLoginId','')
            commit('setOnePost','')
            commit('setLoginer','')
            localStorage.clear()
            router.replace('/adminlogin')
        },
        LoginType({commit},type){
            commit('setLoginType',type)
        },
        LoginId({commit},id){
            commit('setLoginId',id)
        },
        LoginOut({commit}){
            commit('setToken','')
            commit('setLoginType','')
            commit('setLoginId','')
            commit('setOnePost','')
            commit('setLoginer','')
            localStorage.clear()
            router.replace('/login')
        },
        OnePost({commit},data){
            commit('setOnePost',data)
        }
    }
}